from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.core import serializers

from .models import Note
# Create your views here.

def login_check(function):
    def wrap(request, *args, **kwargs):
        if not request.session.get('username') or not request.session.get('userid'):
            return HttpResponseRedirect("/user/login/")
        else:# 更新session
            username = request.session.get('username')
            userid = request.session.get('userid')
            request.session['username'] = username
            request.session['userid'] = userid
        return function(request, *args, **kwargs)
    return wrap

@login_check
def add_note(request):
    
    if request.method == "GET":
        return render(request, 'note/add_note.html')
    elif request.method == "POST":
        
        userid = request.session['userid']
        title = request.POST['title']
        content = request.POST['content']
        
        Note.objects.create(
            title = title,
            content = content,
            user_id = userid
        )
        
        return HttpResponse("添加成功")

@login_check        
def list_view(request):
    
    data_list = Note.objects.all()
    return render(request, 'note/list_note.html', {'data_list': data_list})
    
@login_check
def delete_note(request):
    
    id = request.GET.get('id', None)
    if id is None:
        data_list = Note.objects.all()
        return render(request, "note/list_note.html", {'data_list': data_list})
    try:
        id = int(id)
    except Exception as err:
        data_list = Note.objects.all()
        return render(request, "note/list_note.html", {'data_list': data_list})
    data = Note.objects.get(id = id)
    data.is_active=False
    data.save()
    data_list = Note.objects.all()
    return render(request, "note/list_note.html", {'data_list': data_list})
    
@login_check
def update_note(request):
    
    id = request.GET.get('id', None)
    if id is None:
        data_list = Note.objects.all()
        return render(request, "note/list_note.html", {'data_list': data_list})
    try:
        id = int(id)
    except Exception as err:
        data_list = Note.objects.all()
        return render(request, "note/list_note.html", {'data_list': data_list})
    if request.method == "GET":
        data = Note.objects.get(id = id)
        return render(request, "note/update_note.html", {'note': data})
    elif request.method == "POST":
        title = request.POST.get('title')
        content = request.POST.get('content')
        data = Note.objects.get(id = id)
        data.title = title
        data.content = content
        data.save()
    return HttpResponse("OK")
    
def get_notes(request):
    query = request.GET.get('query', None)
    if query is None:
        raise Http404(request)
    
    try:
        userid = request.session['userid']
        notes = Note.objects.filter(user_id=userid, is_active=True)    
        jsonData = serializers.serialize('json', notes)
        return HttpResponse(jsonData, content_type='application/json')
    except:
        raise Http404(request)